<?PHP
session_start();
include("../config/connect.php");
include("../public/common.php");

if( !isset($_SESSION["gs_user"]) ) 
{
	$url = "Location:login.php";
	header($url);
}

$nPage = SetUrlParamInt("page");
if( $nPage < 0 )
	$nPage = 0;
	
$input_item = CheckUrlParamNoDie( "input_item" );
if( $input_item == "" )
{
	$sql = "select id from user where 1 order by id desc";
}
else
{
	$type = CheckUrlParamNoDie( "type" );
	if( $type == "alipay" )
		$sql = "select id from user where tb_id='$input_item'";
	else if( $type == "tb" )
		$sql = "select id from user where tb='$input_item'";
	else if( $type == "name" )
		$sql = "select id from user where name='$input_item'";
	else
		$sql = "select id from user where tb_id='$input_item'";
}
	
$result = mysql_query( $sql );
$row_num = mysql_num_rows( $result );

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>购物兔</title>
<style type="text/css">
<!--
body,td,th {
	font-family: 宋体;
	font-size: 13px;
	color: #000;
}
body {
	background-color: #FFF;
}
-->
</style>
<script language="javascript">

function OnSaveName( id )
{
	var name = document.getElementById("Name" + id).value;
	var param = "opt=name&name=" + name + "&id=" + id;
	_OnSave( param );
}

function OnSaveTb( id )
{
	var tb= document.getElementById("Tb" + id).value;
	var param = "opt=tb&tb=" + tb + "&id=" + id;
	_OnSave( param );
}

function _OnSave( param )
{
	var input_item = document.getElementById("input_item").value;
	window.location.href= "user_list.php?page=<?PHP echo $nPage; ?>&input_item=" + input_item + "&" + param;
}

function OnFind()
{
	var input_item = document.getElementById("input_item").value;
	window.location.href= "user_list.php?input_item=" + input_item + "&type=" + GetFindType();
}

function OnPageChange( type )
{
	if( type == "pre" )
	{
		window.location.href= "user_list.php?input_item=<?PHP echo $input_item; ?>" + "&page=" + "<?PHP echo $nPage - 1; ?>";
	}
	else if( type == "next" )
	{
		window.location.href= "user_list.php?input_item=<?PHP echo $input_item; ?>" + "&page=" + "<?PHP echo $nPage + 1; ?>";
	}
	else if( type == "first" )
	{
		window.location.href= "user_list.php?input_item=<?PHP echo $input_item; ?>" + "&page=0";
	}
	else if( type == "last" )
	{
		window.location.href= "user_list.php?input_item=<?PHP echo $input_item; ?>" + "&page=<?PHP echo floor($row_num/20); ?>";
	}
}

function GetFindType()
{
	if( document.getElementById("findType_0").checked )
		return "alipay";
		
	if( document.getElementById("findType_1").checked )
		return "tb";
		
	if( document.getElementById("findType_2").checked )
		return "name";
	
	return "alipay";
}

function OnSelectAll( obj )
{
	obj.select();
}
</script>
</head>
<body>
<table width="1069" border="1" align="center" cellpadding="0" cellspacing="0">
	<tr>
    	<td height="25" width="47" align="center">序列号</td>
        <td width="143" align="center">注册时间</td>
        <td width="174" align="center">支付宝帐号</td>
      	<td width="121" align="center">手机</td>
        <td width="67" 	align="center">推荐人ID</td>
        <td width="160" align="center">电子邮件</td>
        <td width="161" align="center">姓名</td>
        <td width="178" align="center">淘宝账号</td>
    </tr>
<?php
$id = SetUrlParamInt("id");
$opt = CheckUrlParamNoDie( "opt" );
if( $opt == "name" && $id != 0 )
{
	$name = CheckUrlParamNoDie( "name" );
	$sql = "update user set name='$name' where id=$id limit 1";
	if( mysql_query( $sql ))
		echo "<script language=\"javascript\">	alert(\"保存成功\");</script>";
	else
		echo "<script language=\"javascript\">	alert(\"保存失败\");</script>";
}
else if( $opt == "tb" && $id != 0 )
{
	$tb = CheckUrlParamNoDie( "tb" );
	$sql = "update user set tb='$tb' where id=$id limit 1";
	if( mysql_query( $sql ))
		echo "<script language=\"javascript\">	alert(\"保存成功\");</script>";
	else
		echo "<script language=\"javascript\">	alert(\"保存失败\");</script>";
}

$limit = "limit ". $nPage*20 .",20";

if( $input_item == "" )
{
	$sql = "select * from user where 1 order by id desc " . $limit;
}
else
{
	$sql = "select * from user where tb_id='$input_item' ". $limit;
	
	$type = CheckUrlParamNoDie( "type" );
	if( $type == "alipay" )
		$sql = "select * from user where tb_id='$input_item '". $limit;
	else if( $type == "tb" )
		$sql = "select * from user where tb='$input_item '". $limit;
	else if( $type == "name" )
		$sql = "select * from user where name='$input_item '". $limit;
	else
		$sql = "select * from user where tb_id='$input_item '". $limit;
}
	
$result = mysql_query( $sql );

while( $result && $row = mysql_fetch_assoc( $result ))
{
	
?>
<tr >
    <td height="25" width="47" align="center"><?PHP echo $row["id"]; ?></td>
    <td width="143" align="center" title=""><?PHP echo $row["reg_time"]; ?></td>
	<td width="174" align="center"><?PHP echo $row["tb_id"]; ?>&nbsp;</td>
	<td width="121" 	align="center"><?PHP echo $row["phone"]; ?>&nbsp;</td>
	<td width="67" align="center"><?PHP echo $row["commend_id"]; ?>&nbsp;</td>
    <td width="160" align="center"><?PHP echo $row["email"]; ?>&nbsp;</td>
    <td width="161" align="center"><input id="Name<?PHP echo $row["id"]; ?>" type="text" value="<?PHP echo $row["name"]; ?>" size="15" onclick="javascript:OnSelectAll(this);"/><a href="#" onclick="javascript:OnSaveName('<?PHP echo $row["id"]; ?>')">保存</a></td>
    <td width="178" align="center"><input id="Tb<?PHP echo $row["id"]; ?>" type="text" value="<?PHP echo $row["tb"]; ?>" size="18" onclick="javascript:OnSelectAll(this);"/><a href="#" onclick="javascript:OnSaveTb('<?PHP echo $row["id"]; ?>')">保存</a></td>
</tr>
<?PHP
}
?>
</table>
<table cellpadding="0" cellspacing="0" border="0" align="center">
	<tr>
   	  <td width="800" height="30" align="center">
   	      <label>
   	        <input type="radio" name="findType" value="0" id="findType_0" <?php if( $type == "alipay" || $type == "" ) echo "checked=\"checked\""; ?> />
   	        支付宝</label>
   	      <label>
   	        <input type="radio" name="findType" value="1" id="findType_1" <?php if( $type == "tb" ) echo "checked=\"checked\""; ?> />
   	        淘宝</label>
          <label>
   	        <input type="radio" name="findType" value="2" id="findType_2" <?php if( $type == "name" ) echo "checked=\"checked\""; ?> />
   	        姓名</label>
   
      &nbsp;<input type="text" value="<?PHP echo $input_item; ?>" id="input_item" size="20" onclick="javascript:OnSelectAll(this);"/><input type="button" onclick="javascript:OnFind();" value="查找" />&nbsp;&nbsp;&nbsp;<a href="#" onclick="javascript:OnPageChange('first');">首页</a>&nbsp;&nbsp;<a href="#" onclick="javascript:OnPageChange('next');">下页</a>&nbsp;&nbsp;<a href="#" onclick="javascript:OnPageChange('pre');">上页</a>&nbsp;&nbsp;<a href="#" onclick="javascript:OnPageChange('last');">末页</a></td>
    </tr>
</table>
</body>
</html>